package com.zhijian.medical.service.impl;

import com.zhijian.medical.dao.local.ExperimentProcessResultMapper;
import com.zhijian.medical.entity.po.ExperimentProcessResult;
import com.zhijian.medical.entity.po.ExperimentProcessResultDetail;
import com.zhijian.medical.entity.vo.request.ExperimentProcessResultDetailSearchVo;
import com.zhijian.medical.entity.vo.request.ExperimentProcessResultSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.request.ExperimentProcessResultSearchVo;
import com.zhijian.medical.entity.vo.response.ExperimentProcessResultDetailVo;
import com.zhijian.medical.entity.vo.response.ExperimentProcessResultVo;
import com.zhijian.medical.service.ExperimentProcessResultDetailService;
import com.zhijian.medical.service.ExperimentProcessResultService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 实验流程-结果业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2022-04-08 23:19:07
 */
@Slf4j
@Service
public class ExperimentProcessResultServiceImpl implements ExperimentProcessResultService {

    @Autowired
    private ExperimentProcessResultMapper mapper;

    @Autowired
    private ExperimentProcessResultDetailService experimentProcessResultDetailService;

    @Override
    public ExperimentProcessResult selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public ExperimentProcessResultVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByExperimentProcessId(Long experimentProcessId) {
        mapper.deleteByExperimentProcessId(experimentProcessId);
    }

    @Transactional(rollbackFor = Exception.class, value = "localTransactionManager")
    @Override
    public void deleteByExperimentProcessIdAndProject(Long experimentProcessId, String project) {
        List<ExperimentProcessResultVo> experimentProcessResultVos = this.selectList(ExperimentProcessResultSearchVo.builder()
                .experimentProcessId(experimentProcessId)
                .project(project)
                .build());
        if (CollectionUtils.isEmpty(experimentProcessResultVos)) {
            return;
        }
        //拼接处所有的结果id
        List<Long> resultIds = experimentProcessResultVos.stream().map(ExperimentProcessResultVo::getId).collect(Collectors.toList());
        //删除结果详情表
        experimentProcessResultDetailService.deleteByResultIds(resultIds);
        mapper.deleteByExperimentProcessIdAndProject(experimentProcessId, project);
    }

    @Transactional(rollbackFor = Exception.class, value = "localTransactionManager")
    @Override
    public void deleteByExperimentProcessIdAndProjectAndDeviceId(Long experimentProcessId, String project, Long deviceId) {
        List<ExperimentProcessResultVo> experimentProcessResultVos = this.selectList(ExperimentProcessResultSearchVo.builder()
                .experimentProcessId(experimentProcessId)
                .project(project)
                .deviceId(deviceId)
                .build());
        if (CollectionUtils.isEmpty(experimentProcessResultVos)) {
            return;
        }
        //拼接处所有的结果id
        List<Long> resultIds = experimentProcessResultVos.stream().map(ExperimentProcessResultVo::getId).collect(Collectors.toList());
        //删除结果详情表
        experimentProcessResultDetailService.deleteByResultIds(resultIds);
        mapper.deleteByExperimentProcessIdAndProjectAndDeviceId(experimentProcessId, project, deviceId);
    }

    @Override
    public void deleteByExperimentProcessIdAndDeviceId(Long experimentProcessId, Long deviceId) {
        List<ExperimentProcessResultVo> experimentProcessResultVos = this.selectList(ExperimentProcessResultSearchVo.builder()
                .experimentProcessId(experimentProcessId)
                .deviceId(deviceId)
                .build());
        if (CollectionUtils.isEmpty(experimentProcessResultVos)) {
            return;
        }
        //拼接处所有的结果id
        List<Long> resultIds = experimentProcessResultVos.stream().map(ExperimentProcessResultVo::getId).collect(Collectors.toList());
        //删除结果详情表
        experimentProcessResultDetailService.deleteByResultIds(resultIds);
        mapper.deleteByExperimentProcessIdAndDeviceId(experimentProcessId, deviceId);
    }

    @Override
    public Long insert(ExperimentProcessResult experimentProcessResult) {
        Date now = new Date();
        experimentProcessResult.setCreateTime(now);
        experimentProcessResult.setUpdateTime(now);
        return mapper.insert(experimentProcessResult) > 0 ? experimentProcessResult.getId() : 0L;
    }

    @Transactional(rollbackFor = Exception.class, value = "localTransactionManager")
    @Override
    public Long insertOrUpdate(ExperimentProcessResultSaveOrUpdateVo experimentProcessResultSaveOrUpdateVo) {
        Long experimentProcessId = experimentProcessResultSaveOrUpdateVo.getExperimentProcessId();
        this.deleteByExperimentProcessId(experimentProcessId);
        experimentProcessResultDetailService.deleteByExperimentProcessId(experimentProcessId);
        ExperimentProcessResult experimentProcessResult = new ExperimentProcessResult();
        BeanUtils.copyProperties(experimentProcessResultSaveOrUpdateVo, experimentProcessResult);
        Long resultId = this.insert(experimentProcessResult);
        experimentProcessResultSaveOrUpdateVo.getExperimentProcessResultDetailList().forEach(experimentProcessResultDetailSaveOrUpdateVo -> {
            experimentProcessResultDetailSaveOrUpdateVo.setExperimentProcessId(experimentProcessId);
            experimentProcessResultDetailSaveOrUpdateVo.setResultId(resultId);
            ExperimentProcessResultDetail experimentProcessResultDetail = new ExperimentProcessResultDetail();
            BeanUtils.copyProperties(experimentProcessResultDetailSaveOrUpdateVo, experimentProcessResultDetail);
            experimentProcessResultDetailService.insert(experimentProcessResultDetail);
        });
        return resultId;
    }

    @Override
    public boolean update(ExperimentProcessResult experimentProcessResult) {
        Date now = new Date();
        experimentProcessResult.setUpdateTime(now);
        return mapper.updateByPrimaryKey(experimentProcessResult) != 0;
    }

    @Override
    public Integer count(ExperimentProcessResultSearchVo experimentProcessResultSearchVo) {
        return mapper.count(experimentProcessResultSearchVo);
    }

    @Override
    public List<ExperimentProcessResultVo> selectList(ExperimentProcessResultSearchVo experimentProcessResultSearchVo) {
        List<ExperimentProcessResultVo> experimentProcessResultVos = mapper.selectList(experimentProcessResultSearchVo);
        if (CollectionUtils.isEmpty(experimentProcessResultVos)) {
            return new LinkedList<>();
        }
        experimentProcessResultVos = experimentProcessResultVos.stream().map(experimentProcessResultVo -> {
            List<ExperimentProcessResultDetailVo> experimentProcessResultDetailVos = experimentProcessResultDetailService.selectList(ExperimentProcessResultDetailSearchVo.builder()
                    .resultId(experimentProcessResultVo.getId())
                    .build());
            experimentProcessResultVo.setExperimentProcessResultDetailList(experimentProcessResultDetailVos);
            return experimentProcessResultVo;
        }).collect(Collectors.toList());
        return experimentProcessResultVos;
    }

    @Override
    public Page<ExperimentProcessResultVo> selectPage(ExperimentProcessResultSearchVo experimentProcessResultSearchVo) {
        //先直接查总数
        int count = count(experimentProcessResultSearchVo);
        //分页
        Page<ExperimentProcessResultVo> page = new Page<>(true, count, experimentProcessResultSearchVo.getPageNum(), experimentProcessResultSearchVo.getPageSize());
        experimentProcessResultSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<ExperimentProcessResultVo> responseVos = this.selectList(experimentProcessResultSearchVo);
        page.setData(responseVos);
        return page;
    }

}